package de.tabuma.sso.privileg;

import de.tabuma.sso.*;
import de.tabuma.sso.group.*;
import de.tabuma.sso.project.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.user.*;
import de.tabuma.shopping.logik.*;
import java.util.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class PrivilegWebservice {
    public static int create(Privileg privileg, String ssoId) throws Exception {
        int groupId = PrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PRIVILEG_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        return PrivilegFacade.create(privileg, user.getName());
    }
    
    public static void update(Privileg privileg, String ssoId) throws Exception {
        int groupId = PrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PRIVILEG_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Privileg oldPrivileg = PrivilegFacade.get(privileg.getId().intValue());
        PrivilegWebserviceLib.userHasPrivilegForPrivileg(oldPrivileg, ssoId);
        PrivilegWebserviceLib.userHasPrivilegForPrivileg(privileg, ssoId);
        
        PrivilegFacade.update(privileg, user.getName());
    }
    
    public static Privileg get(int id, String ssoId) throws Exception {
        int groupId = PrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PRIVILEG_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Privileg privileg = PrivilegFacade.get(id);
        PrivilegWebserviceLib.userHasPrivilegForPrivileg(privileg, ssoId);
        return privileg;
    }
    
    public static void remove(int id, String ssoId) throws Exception {
        int groupId = PrivilegWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PRIVILEG_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Privileg privileg = PrivilegFacade.get(id);
        PrivilegWebserviceLib.userHasPrivilegForPrivileg(privileg, ssoId);
        PrivilegFacade.remove(id, user.getName());
    }
    
    public static List<Privileg> getList(int projectId, String ssoId) throws Exception {

        int groupId = ProjectWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PRIVILEG_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Project project = ProjectFacade.get(projectId);
        ProjectWebserviceLib.userHasPrivilegForProject(project, ssoId);

        return PrivilegFacade.getList(projectId);
    }

}
